package Stream.中间操作符;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class flatMap {

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("ab","cd","ef","gh");
        // flatMap比map会多出一个扁平化处理：
        //（1）flatMap::getCharacterByString 会把元素分成：[a,b],[c,d],[e,f],[g,h]
        // (2) 然后把这些元素都合在一起：[a,b,,c,d,e,f,g,h]
        Stream<Character> characterStream = strings.stream().flatMap(flatMap::getCharacterByString);
        characterStream.forEach(character -> System.out.print(character));

        // map没有扁平化处理
        Stream<Stream<Character>> streamStream = strings.stream().map(flatMap::getCharacterByString);
        streamStream.forEach(character -> System.out.print(character));

    }

    // 把的一个字符串的字符，依次放入到集合中，然后把list集合以流的形式返回
    public static Stream<Character> getCharacterByString(String art)
    {
        List<Character> characterList = new ArrayList<>();
        char[] chars = art.toCharArray();
        for (char aChar : chars) {
            characterList.add(aChar);
        }
        return characterList.stream();
    }
}
